<template>
  <div class="home">
    <div class="home-header">
      <div class="home-header-top">
        <div class="home-header-top-menu" @click="showUser=true"></div>
        <div class="home-header-top-tabs">
          <div
            v-for="(subitem, subindex) in menuSub"
            @click="checkoutMenu(subitem, subindex)"
            :key="subindex"
            :class="{ 'tabs-item-active': subitem.checked }"
            class="home-header-top-tabs-item"
          >
            {{ subitem.title }}
          </div>
        </div>
        <div class="home-header-top-search" @click="toSearchPage"></div>
      </div>
    </div>
    <div class="home-container">
      <router-view v-slot="{ Component }">
        <keep-alive>
          <component :is="Component"></component>
        </keep-alive>
      </router-view>
    </div>
    <van-popup v-model:show="showUser" position="left" :style="{height:'100%',width:'70%'}">
      <User />
    </van-popup>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, watch,defineAsyncComponent } from "vue";
import { useCurrentInstance } from "@/utils/hooks";

export interface IMenu {
  title: string;
  checked: boolean;
  pathName: string;
}

export interface IStateProps {
  menuSub: IMenu[];
  showUser: boolean;
}

export default defineComponent({
  name: "Home",
  components: {
    User: defineAsyncComponent({
      loader: ()=>import('./components/user.vue')
    })
  },
  setup() {
    const _this = useCurrentInstance();
    const state = reactive<IStateProps>({
      menuSub: [
        { title: "推荐", checked: true, pathName: "/home/recommend" },
        { title: "我的", checked: false, pathName: "/home/mine" },
      ],
      showUser: false
    });
    // 切换顶部 推荐/我的
    const checkoutMenu = (item: IMenu, index: number) => {
      const activeIndex = state.menuSub.findIndex((op) => op.checked);
      if (activeIndex === index) return;
      _this.$router.push({
        path: item.pathName,
      });
      state.menuSub = state.menuSub.map((subItem: IMenu, num: number) => {
        return {
          ...subItem,
          checked: index === num ? true : false,
        };
      });
    };
    /** 去搜索页 */
    const toSearchPage = () => {
      _this.goPage("search", null);
    };
    /** 设置顶部 */
    const setSubTitle = (path: string) => {
      const activeIndex = state.menuSub.findIndex(
        (op: IMenu) => op.pathName === path
      );
      if (activeIndex !== state.menuSub.findIndex((op: IMenu) => op.checked)) {
        state.menuSub = state.menuSub.map((item, index) => {
          return {
            ...item,
            checked: index === activeIndex ? true : false,
          };
        });
      }
    };
    setSubTitle(_this.$route.path)
    watch(
      () => _this.$route,
      (route) => {
        setSubTitle(route.path)
      }
    );
    return {
      ...toRefs(state),
      checkoutMenu,
      toSearchPage,
      setSubTitle
    };
  },
});
</script>

<style lang="scss" scoped>
.home {
  @extend .screen_page;
  background: #fff;
  .home-header {
    .home-header-top {
      height: 42px;
      box-sizing: border-box;
      padding: 0 15px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #f0f0f0;
      .home-header-top-menu,
      .home-header-top-search {
        width: 26px;
        height: 26px;
        background-size: 100% 100%;
        background-repeat: no-repeat;
        background-image: url("./images/menu.svg");
      }
      .home-header-top-search {
        background-image: url("./images/search.svg");
      }
      .home-header-top-tabs {
        display: flex;
        height: 30px;
        justify-content: center;
        align-items: center;
        font-size: 17px;
        color: #707070;
        font-weight: 600;
        .home-header-top-tabs-item {
          height: 100%;
          line-height: 30px;
          padding: 0 10px;
          &.tabs-item-active {
            @include systemFontColor()
          }
        }
      }
    }
  }
  .home-container {
    height: calc(100% - 42px);
    /** 给音乐播放器留空间 */
    padding-bottom: 50px;
    box-sizing: border-box;
  }
}
</style>
